package com.demo.cms.action.manage;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import com.demo.Constants;
import com.demo.core.model.Config;
import com.demo.core.model.ConfigItem;
import com.demo.core.model.Form;
import com.demo.core.model.FormItem;
import com.demo.core.model.FormItemExtend;
import com.demo.core.model.FormSource;
import com.demo.core.model.FormSourceParam;
import com.demo.core.model.Site;
import com.demo.core.service.base.ConfigItemService;
import com.demo.core.service.base.ConfigService;
import com.demo.core.service.base.FormService;
import com.demo.core.service.base.FormSourceService;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * 文章的自定义表单Action
 * 自定义表单服务(模型服务)
 * 1.文章模型管理
 * 
 * @author ryuu.kk
 *
 */
@Function(id = 105100, pid = 10, code = "manage.formAction")
public class FormArticleAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 4912117741243678198L;
	
	/**
	 * 自定义表单主键ID
	 */
	private Long id;

	/**
	 * 站点ID
	 */
	private Long siteId;
	
	/**
	 * 自定义表单模型
	 */
	private Form form;
	
	/**
	 * 自定义表单列表
	 */
	private List<Form> formList;
	
	/**
	 * 自定义表单项
	 */
	private FormItem formItem;
	
	/**
	 * 自定义表单项列表
	 */
	private List<FormItem> formItemList;
	
	/**
	 * 自定义表单项列表
	 * 之所有有两个列表是由于,一些扩展项是根据Article表中的实体字段来的
	 * 而一些自定义表单项是根据ArticleExtend表中扩展来的.
	 */
	private List<FormItem> formCustomItemList;
	
	/**
	 * 配置模型
	 */
	private Config config;
	
	/**
	 * 配置项模型
	 */
	private ConfigItem configItem;
	
	/**
	 * 配置服务
	 */
	private ConfigService configService;
	
	/**
	 * 配置项服务
	 */
	private ConfigItemService configItemService;
	
	/**
	 * 自定义表单资源
	 */
	private FormSource formSource;
	
	/**
	 * 自定义表单资源参数
	 */
	private FormSourceParam formSourceParam;
	
	/**
	 * 自定义表单资源参数列表
	 */
	private List<FormSourceParam> formSourceParamList;
	
	/**
	 * 自定义表单服务
	 */
	private FormService formService;
	
	/**
	 * 自定义表单资源服务
	 */
	private FormSourceService formSourceService;

	/**
	 * 自定义表单列表
	 * (文章类型列表)
	 * @return String
	 */
	@Function(id = 105110, pid = 105100, code = "manage.formAction.list")
	public String list() {
		
		Site site = MultisiteContextHolder.getSite();
		this.siteId = site.getId();
		// 取得文章配置类型
		this.config = configService.query(siteId, Constants.CONFIG_ARTICLE_TYPE);
		
		if (this.config != null && !config.getSiteId().equals(this.siteId)) {
			addActionError("配置类型不存在或跨站点读取配置类型信息.");
			return INPUT;
		}
		//this.formList = formService.findFormList(siteId);
		return SUCCESS;
	}
	
	/**
	 * 创建自定义表单
	 * (创建文章类型)
	 * @return String
	 */
	@Function(id = 105120, pid = 105100, code = "manage.formAction.create")
	public String create() {
		
		//this.configItem = configItemService.find(id);
		// configItemService.save(this.configItem);
		//Long formId = Long.parseLong(configItem.getValue());
		return SUCCESS;
	}
	
	/**
	 * 保存自定义表单
	 * @return String
	 */
	@Function(id = 105130, pid = 105100, code = "manage.formAction.save")
	public String save() {
		// 得到当前访问的站点
		Site site = getSite();
		Long siteId = site.getId();
		
		// 读取自定义表单相关的配置模型
		this.config = configService.find(siteId, Constants.CONFIG_ARTICLE_TYPE);
		
		form.setSiteId(siteId);
		form.setType("IN");
		form.setStatus("RWD");
		form.setI18n(config.getI18n());
		// 存储自定义表单
		id = formService.save(form);
		
		this.configItem = new ConfigItem();
		configItem.setConfigId(config.getId());
		configItem.setEnabled(Boolean.TRUE);
		configItem.setI18n(config.getI18n());
		configItem.setName(form.getName());
		configItem.setText(form.getTitle());
		configItem.setEnabled(form.getEnabled());
		configItem.setKey(Constants.CONFIG_ARTICLE_FORM_ID);
		// 将Form的ID放入配置项中
		configItem.setValue(String.valueOf(id));
		// 新建配置项
		configItemService.save(configItem);
		
		addActionMessage("自定义表单创建成功.");
		
		return CALLBACK;
	}
	
	/**
	 * 编辑自定义表单
	 * @return String
	 */
	@Function(id = 105140, pid = 105100, code = "manage.formAction.edit")
	public String edit() {
		
		this.configItem = configItemService.find(id);
		// configItemService.save(this.configItem);
		Long formId = Long.parseLong(configItem.getValue());
		
		// 根据配置ID查找自定义表单Form和自定义表单包含的元素
		this.form = formService.findForm(formId);
		// 自定义表单项(该项目将生成一个表单,对应T_CMS_ARTICLE表的字段)
		this.formItemList = formService.findFormItemList(formId, Constants.FORM_GROUP_NORMAL);
		// 自定义表单项(该项目为扩展项,将生成一个表单,表单提交后将存在T_CMS_ARTICLE表中)
		this.formCustomItemList = formService.findFormItemList(formId, Constants.FORM_GROUP_EXTEND);
		
		return SUCCESS;
	}
	
	/**
	 * 更新自定义表单
	 * @return String
	 */
	@Function(id = 105150, pid = 105100, code = "manage.formAction.update")
	public String update() {
		
		// 取得配置信息后更新Form
		configItem = configItemService.find(id);
		Long formId = Long.parseLong(configItem.getValue()); 
		formService.updateForm(formId, form.getName(), form.getTitle());
		formService.updateFormStatus(formId, form.getEnabled(), "RWD");
		configItemService.updateStatus(id, form.getEnabled(), "RWD");
		configItemService.update(id, form.getName(), form.getId() + "", form.getTitle());
		if (form != null && form.getFormItemList() != null) {
			for (FormItem item : form.getFormItemList()) {
				formService.updateFormItemProperties(item.getId(), item.getName(), item.getTitle(), item.getAction());
				formService.updateFormItemStatus(item.getId(), item.getEnabled(), "RWD", item.getPriority());
			}
			addActionMessage("自定义表单更新成功.");
		}
		
		return CALLBACK;
	}
	
	/**
	 * 删除自定义表单
	 * @return String
	 */
	@Function(id = 105160, pid = 105100, code = "manage.formAction.delete")
	public String delete() {
		// 取得配置信息
		configItem = configItemService.find(id);
		Long formId = Long.parseLong(configItem.getValue());
		// 删除表单
		formService.deleteForm(formId);
		// 删除配置
		configItemService.delete(id);
		
		addActionMessage("表单删除成功");
		return CALLBACK;
	}
	
	/**
	 * 新建自定义表单项
	 * @return String
	 */
	@Function(id = 105121, pid = 105120, code = "manage.formAction.create.createItem")
	public String createItem() {
		
		return SUCCESS;
	}
	
	/**
	 * 保存自定义表单项
	 * @return String
	 */
	@Function(id = 105131, pid = 105130, code = "manage.formAction.save.saveItem")
	public String saveItem() {
		
		Long siteId = getSite().getId();
		Enumeration<?> enumer = getRequest().getParameterNames();
		List<FormItemExtend> extendList = new ArrayList<FormItemExtend>();
		while (enumer.hasMoreElements()) {
			String name = (String) enumer.nextElement();
			if (name.startsWith(Constants.FORM_ITEM_PREFIX)) {
				// 得到输入的KEY名
				String key = name.replace(Constants.FORM_ITEM_PREFIX, "").trim();
				String value = getRequest().getParameter(name);
				FormItemExtend extend = new FormItemExtend();
				extend.setKey(key);
				extend.setValue(value);
				//extend.setItemId(itemId);
				extendList.add(extend);
			}
		}
		formItem.setSiteId(siteId);
		// 需要formID,创建时需要新增Form.
		formItem.setFormId(id);
		formItem.setStatus("RWD");
		// 自定义表单只能增加一些业务的扩展属性表内容,这里设置区分
		formItem.setGroupType(Constants.FORM_GROUP_EXTEND);
		formItem.setEnabled(Boolean.TRUE);
		
		Long itemId = formService.save(formItem);
		// 增加扩展属性部分
		for (FormItemExtend extend : extendList) {
			extend.setItemId(itemId);
			formService.save(extend);
		}
		
		addActionMessage("表单项保存成功");
		return CALLBACK;
	}
	/**
	 * 删除自定义表单项
	 * @return String
	 */
	@Function(id = 105161, pid = 105160, code = "manage.formAction.delete.deleteItem")
	public String deleteItem() {
		
		formService.deleteFormItem(id);
		addActionMessage("自定义表单项删除成功.");
		return CALLBACK;
	}
	
	/**
	 * 编辑自定义表单项
	 * @return String
	 */
	@Function(id = 105141, pid = 105140, code = "manage.formAction.edit.editItem")
	public String editItem() {
		//formService.u
		// 获得表单项模型,包含扩展属性
		this.formItem = formService.queryFormItem(id);
		
		return SUCCESS;
	}
	
	/**
	 * 更新自定义表单项
	 * @return String
	 */
	@Function(id = 105151, pid = 105150, code = "manage.formAction.update.updateItem")
	public String updateItem() {
		//formService.u
		// 获得表单项模型,包含扩展属性
		// this.formItem = formService.queryFormItem(id);
		Long itemId = formItem.getId();
		Enumeration<?> enumer = getRequest().getParameterNames();
		List<FormItemExtend> extendList = new ArrayList<FormItemExtend>();
		while (enumer.hasMoreElements()) {
			String name = (String) enumer.nextElement();
			if (name.startsWith(Constants.FORM_ITEM_PREFIX)) {
				// 得到输入的KEY名
				String key = name.replace(Constants.FORM_ITEM_PREFIX, "").trim();
				String value = getRequest().getParameter(name);
				FormItemExtend extend = new FormItemExtend();
				extend.setKey(key);
				extend.setValue(value);
				extend.setItemId(itemId);
				extendList.add(extend);
			}
		}
		// formItem.setFormItemExtendList(extendList);
		// 更新表单的HTML部分
		formService.updateFormItemHtml(itemId, formItem.getHtmlId(), formItem.getHtmlElement(), formItem.getHtmlType(), formItem.getInnerHtml());
		// 更新表单的属性部分(做不为空验证)
		formService.updateFormItemProperties(itemId, formItem.getName(), formItem.getTitle(), formItem.getAction());
		// 更新表单的数据源绑定部分
		formService.updateFormItemSource(itemId, formItem.getDs(), formItem.getBindName(), formItem.getBindValue());
		
		// 删除扩展属性
		formService.deleteFormItemExtends(itemId);
		// 更新表单的扩展属性部分
		for (FormItemExtend extned : extendList) {
			formService.save(extned);
		}
		
		return CALLBACK;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	public FormItem getFormItem() {
		return formItem;
	}

	public void setFormItem(FormItem formItem) {
		this.formItem = formItem;
	}

	public FormSource getFormSource() {
		return formSource;
	}

	public void setFormSource(FormSource formSource) {
		this.formSource = formSource;
	}

	public FormSourceParam getFormSourceParam() {
		return formSourceParam;
	}

	public void setFormSourceParam(FormSourceParam formSourceParam) {
		this.formSourceParam = formSourceParam;
	}

	public List<Form> getFormList() {
		return formList;
	}

	public List<FormItem> getFormItemList() {
		return formItemList;
	}

	public void setFormItemList(List<FormItem> formItemList) {
		this.formItemList = formItemList;
	}

	public List<FormItem> getFormCustomItemList() {
		return formCustomItemList;
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public ConfigItem getConfigItem() {
		return configItem;
	}

	public void setConfigItem(ConfigItem configItem) {
		this.configItem = configItem;
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	public void setConfigItemService(ConfigItemService configItemService) {
		this.configItemService = configItemService;
	}

	public void setFormSourceParamList(List<FormSourceParam> formSourceParamList) {
		this.formSourceParamList = formSourceParamList;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public void setFormSourceService(FormSourceService formSourceService) {
		this.formSourceService = formSourceService;
	}
}
